<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Rep\FinishProductRep;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\FinishProductExport;

class FinishProduct extends Controller
{
    //成品管理

    protected $rep;

    public function __construct(Request $request)
    {
        $this->rep = new FinishProductRep();
    }

    /**
     * 获取成品列表
     * @param Request $request
     */
    public function getFinishProductList(Request $request){
        $params = $request->all();
        $export = $request->get('export')??0;
        if($export == 1){
            try{
                $filename = date('ymdhis').'.xlsx';
                $export = new FinishProductExport($params);
                return Excel::download($export,$filename,\Maatwebsite\Excel\Excel::XLSX,[
                    'Content-Type'=> 'application/octet-stream',
                    'Content-Disposition'=> 'attachment; filename='.$filename,
                    'Content-Transfer-Encoding'=> 'binary',
                ]);
            }catch(\Exception $exception){
                return re(0,[],$exception->getMessage());
            }
        }
        $data = $this->rep->getFinishProductListRep($params);
        return re(0,$data,'success');
    }

    /**
     * 获取备货列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getStockUpList(Request $request){
        $params = $request->all();
        $data = $this->rep->getStockUpListRep($params);
        return re(0,$data,'success');
    }

    /**
     * 获取当前产品可调货的列表
     */
    public function getStockAllocationList(Request $request){
        $params = $request->all();
        $data = $this->rep->getStockAllocationListRep($params);
        return re(0,$data,'success');
    }

    /**
     * 调货
     * @param Request $request
     */
    public function stockAllocation(Request $request){
        $params = $request->all();
        $data = $this->rep->stockAllocationRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 调货记录
     */
    public function getStockAllocationLog(Request $request){
        $params = $request->all();
        $data = $this->rep->getStockAllocationLogRep($params);
        return re(0,$data,'success');
    }

    /**
     * 设置发货状态
     */
    public function setDeliveryStatus(Request $request){
        $params = $request->all();
        $data = $this->rep->setDeliveryStatusRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 发货
     */
    public function DeliverGoods(Request $request){
        $params = $request->all();
        $data = $this->rep->DeliverGoodsRep($params);
        return re(0,$data,'success');
    }

    /**
     * 扫码发货
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deliveryByScanningCode(Request $request){
        $params = $request->all();
        $data = $this->rep->deliveryByScanningCodeRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 取消待发货
     */
    public function cancelDelivery(Request $request){
        $params = $request->all();
        $data = $this->rep->cancelDeliveryRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }




    /**
     * 将成品移动到备货||将备货移动到成品
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStockUpStatus(Request $request){
        $params = $request->only(['id','order_id','is_stock_up','customer_id']);
        $data = $this->rep->setStockUpStatusRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 扣款
     */
    public function deductMoney(Request $request){
        $params = $request->all();
        $data = $this->rep->deductMoneyRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }
    /**
     * 修改成品列表中的件数
     */
    public function updateFinishProductNum(Request $request){
        $params = $request->all();
        $data = $this->rep->updateFinishProductNumRep($params);
        return re(0,$data,'success');
    }



    /**
     * 成品删除
     */
    public function delFinishProduct(Request $request){
        $params = $request->all();
        $data = $this->rep->delFinishProductRep($params);
        return re(0,$data,'success');
    }

    /**
     * 扫码盘点
     */
    public function scanQRCodesCheck(Request $request){
        $params = $request->all();
        $data = $this->rep->scanQRCodesCheckRep($params);
        return re(0,$data,'success');
    }


}
